package crp.core.resource.domain.repositories;

import com.qwlabs.lang.C2;
import com.qwlabs.panache.Where;
import crp.core.resource.domain.models.ResourceEntity;
import crp.core.resource.domain.models.ResourceQuery;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;

import javax.enterprise.context.ApplicationScoped;
import java.util.Collection;
import java.util.stream.Stream;

@ApplicationScoped
public class ResourceRepository implements PanacheRepositoryBase<ResourceEntity, String> {
    public Stream<ResourceEntity> find(ResourceQuery filter) {
        if (filter == null) {
            return findAll().stream();
        }
        return getEntityManager()
                .createNativeQuery(filter.buildNativeSql(), ResourceEntity.class)
                .getResultStream();
    }

    public Stream<ResourceEntity> findByIds(Collection<String> ids) {
        if (C2.isEmpty(ids)) {
            return Stream.empty();
        }
        Where where = Where.create().and("id in (:ids)", "ids", ids);
        return find(where.get(), where.getParameters()).stream();
    }
}
